<?php
class Answer_Component_Controller_Question_View extends Phpfox_Component
{
    public function process()  {

        $sLink = "answer.question.view";
		$categories = Phpfox::getService('marketplace.category')->getForBrowse();
		foreach($categories as $key=>$cat){
			reset($arrEx);
			$endName = '';
			$arrEx = array();
			if($cat['name'][0]=='{'){
				$arrEx = explode("var=",$cat['name']);
				$endName = substr($arrEx[1], 6, -7);
				$categories[$key]['name'] = Phpfox::getPhrase($endName);
			}
		}
		$this->template()->assign(array(
					'categories' => $categories)
		);
        // filer menu
        Phpfox::getService('answer.question')->getSectionMenu();

        /*if ($iAnswerId = $this->request()->getInt('correct')) {

            $iQuestionId = Phpfox::getService('answer.point')->correct($iAnswerId);
            $aCondition = 'a.id =' . $iAnswerId;
            $aAnswer = Phpfox::getService('answer.answer')->getForEdit($aCondition);
            Phpfox::getService('notification.process')->add('answer_answer_correct', $iAnswerId, $aAnswer['data']['user_id'], Phpfox::getUserId());
            $aVal['id'] = $iQuestionId;
            $aVal['status'] = 1;
            Phpfox::getService('answer.question')->saveDatabase($aVal);
            $this->url()->send($sLink, array('id' => $iQuestionId), Phpfox::getPhrase('answer.change_this_answer_is_correct_successfully'));
        }
        if ($iAnswerId = $this->request()->getInt('uncorrect')) {
            $iQuestionId = Phpfox::getService('answer.point')->uncorrect($iAnswerId);
            $aCondition ='a.id = ' . $iAnswerId;
            $aAnswer = Phpfox::getService('answer.answer')->getForEdit($aCondition);
            $aVal['id'] = $iQuestionId;
            $aVal['status'] = 0;
            Phpfox::getService('answer.question')->saveDatabase($aVal);
            $this->url()->send($sLink, array('id' => $iQuestionId), Phpfox::getPhrase('answer.this_answer_is_not_correct'));
        }
        if ($iAnswerId = $this->request()->getInt('delete-answer')) {
            $aCondition = 'a.id = '. $iAnswerId;
            $aAnswer = Phpfox::getService('answer.answer')->getForEdit($aCondition);
            $iQuestionId = $aAnswer['data']['question_id'];
            Phpfox::getService('answer.answer')->delete($iAnswerId);
            $this->url()->send($sLink, array('id' => $iQuestionId), Phpfox::getPhrase('answer.this_answer_deleted_susscessfully'));
        }*/
        if ($aVal = $this->request()->getArray('val')) {
            Phpfox::isUser(true);
            $aVal['id'] = '';
            $aVal['user_id'] = Phpfox::getUserId();

            Phpfox::getService('answer.answer')->saveDatabase($aVal);
            $aCondition = 'q.id = ' . $aVal['question_id'];
            $aQuestion = Phpfox::getService('answer.question')->getForEdit($aCondition);
            $iUserID = $aQuestion['data']['user_id'];
            Phpfox::getService('notification.process')->add('answer_question_answer', $aVal['question_id'], $iUserID, Phpfox::getUserId());
            $this->url()->send($sLink, array('id' => $aVal['question_id']), Phpfox::getPhrase('answer.answer_a_question_successfully'));
        }

        $iId = $this->request()->getInt('id');
        $aCondition = array('id = ' . $iId);
        if (!Phpfox::getService('answer.question')->isExist($aCondition)) {

            $this->url()->send("answer", array('id' => $iId), Phpfox::getPhrase('answer.this_question_not_found'));
            return false;
        }

        Phpfox::getService('answer.question')->incView($iId);
        $aCondition = array('q.id = ' . $iId);
        $aQuestion = Phpfox::getService('answer.question')->getForEdit($aCondition);
		
        $aQuestion = $aQuestion['data'];
        /*$aTags = array();
        if ($aQuestion['tag'] != "") {
            $aTags = explode(",", $aQuestion['tag']);
            foreach ($aTags as $iKey => $Val) {
                $aTags[$iKey] = trim($Val);
            }
        }*/
        $this->template()
            ->setBreadcrumb(Phpfox::getPhrase('answer.answers'), $this->url()->makeUrl('answer'), false);
        if ($aQuestion['main_category']['id'] != 0) {
            $this->template()
                ->setBreadcrumb($aQuestion['main_category']['name'], $this->url()->makeUrl('answer.category', array('id' => $aQuestion['main_category']['id'])), false);

        }
        $this->template()
            ->setBreadcrumb($aQuestion['category_name'], $this->url()->makeUrl('answer.category', array('id' => $aQuestion['category_id'])), false);


        /*$aCondition = array(
            'a.question_id ' => $iId,
            'a.id <> ' => Phpfox::getService('answer.point')->getCorrectAnswer($iId)
        );
        $iLimit = Phpfox::getParam('answer.number_answers_per_page') == 0 ? 10 : Phpfox::getParam('answer.number_answers_per_page');
        if (!$iPage = $this->request()->get('page')) {
            $iPage = 1;
        }
        if ((!$sOrder = $this->request()->get('sort')) || ($this->request()->get('sort') == "created")) {
            $sOrder = "a.created desc";
            $sSelect = "";
        } else {
            $sOrder = "vote desc";
            $sSelect = 'selected="selected"';
        }

        $aAnswer = Phpfox::getService('answer.answer')->get('a.*', $aCondition, $sOrder, $iPage, $iLimit);

        Phpfox::getLib('pager')->set(array('page' => $iPage, 'size' => $iLimit, 'count' => $aAnswer['count']));
        $iTotal = $aAnswer['count'];
        $aAnswer = $aAnswer['data'];

        $aCondition = array(
            'a.question_id ' =>  $iId,
            'a.id ' => Phpfox::getService('answer.point')->getCorrectAnswer($iId)
        );

        $aCorrectAnswer = Phpfox::getService('answer.answer')->get('a.*', $aCondition);
        if ($aCorrectAnswer['count'] != '0') {
            $aCorrectAnswer = $aCorrectAnswer['data'][0];
        } else {
            $aCorrectAnswer = array();
        }*/

        $this->template()
            ->setTitle($aQuestion['title'])
            ->setBreadCrumb($aQuestion['title'], "", true)
            ->setHeader(array(
            'answer.css' => 'module_answer',
            'actions.js' => 'module_answer',
            'comment.css' => 'style_css',
            'feed.js' => 'module_feed'
        ))
            ->assign(array(
            'aQuestion' => $aQuestion,
            //'aTags' => $aTags,
            //'aCorrectAnswer' => $aCorrectAnswer,
            //'aAnswers' => $aAnswer,
            'total' => $iTotal,
            'iUserId' => Phpfox::getUserId(),
            'sSelect' => $sSelect
        ));

    }
}